<?php

// 后台首页类

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;


class IndexController extends Controller
{
	// 后台首页
	public function index()
    {
		// 读取全部日志
		$log_info = $this -> read_All_Log();
        return view( "index",[ 'log_info'=>$log_info ] );
    }
	
	
	// 打开 网站地图
	public function showMap()
	{
        // 开始
        // 获取路由
        $route_array = request()->route()->getAction();
        $controller = explode("\\",$route_array['controller']);
        $result = explode("@",$controller[3]);
        // 控制器 	$result[0];
        // 方法 	$result[1];
        $auth_final = '';
        foreach( Session::get('auths') as $key=> $auth_info )
        {
            // 符合条件
            if( $auth_info['controller']==$result[0] && $auth_info['func']==$result[1] )
            {
                $auth_final = true;
                break;
            }else
            {
                $auth_final = false;
            }
        }
        // 权限验证失败
        if( !$auth_final )
        {
            return redirect('/')->with( ['authinfo'=>'没有此项权限'] );
        }
        // 结尾
		$str = "/newsList/all/tag"."\r\n";
		$str .= "/photoList"."\r\n";
		$str .= "/footClub"."\r\n";
		$str .= "/recommendlist"."\r\n";
		$str .= "/database"."\r\n";
		$str .= "/footClub/16"."\r\n";
		$str .= "/footClub/17"."\r\n";
		$str .= "/footClub/18"."\r\n";
		$str .= "/newsList/2/tag"."\r\n";
		$str .= "/newsList/3/tag"."\r\n";
		$str .= "/newsList/4/tag"."\r\n";
		$str .= "/newsList/5/tag"."\r\n";
		$str .= "/newsList/6/tag"."\r\n";
		$str .= "/photoCategoryList/13"."\r\n";
		$str .= "/photoCategoryList/14"."\r\n";
		$str .= "/database/2"."\r\n";
		$str .= "/database/3"."\r\n";
		$str .= "/database/4"."\r\n";
		$str .= "/database/5"."\r\n";
		$str .= "/database/6"."\r\n";
		$str .= "/database/128"."\r\n";
		return view( 'web.map',['str'=>$str] );
	}
	
	// 生成 网站 地图
	public function createMap( Request $request )
	{
		// 包含Ip
		$data = $request -> except('_token');
		
		// $filename = "sitemap";
		// 浏览器信息
		// $user_agent = $_SERVER['HTTP_USER_AGENT'];
		// if(preg_match("/MSIE/", $user_agent)) {
			// $filename = urlencode($filename);
		// }
		
		//设置 csv header
		// header("Content-type:text/csv");
		// header("Content-Disposition:attachment;filename=".$filename.".txt");
		// header('Cache-Control:must-revalidate,insert-check=0,pre-check=0');
		// header('Expires:0');
		// header('Pragma:public');
		
		//打开标准输出
		// $resource = fopen('php://output', 'w');
		$resource = fopen("../../zhizhu/public/sitemap.txt", 'w');
		
		if( empty($data['ip']) )
		{
			// 默认
			$server = "http://www.zhizhu.tv";
		}else
		{
			$server = $data['ip'];
		}
		// 首页 地图
		fwrite($resource,$this->encoding_convert($server."\r\n"));
		if( !empty($data['poip']) )
		{
			$poip = explode( "\r\n",$data['poip'] );
			foreach( $poip as $key => $url )
			{
				// 固定url
				fwrite($resource,$this->encoding_convert( $server.$url."\r\n" ));
			}
		}
		
		// 2.资讯地图
		$news = DB::table('news')
				-> select('id')
				-> orderBy('id','desc')
				-> get();
		foreach( $news as $key => $news_info )
		{
			//每进来一个数组，先写入一个换行
			fwrite($resource, $this->encoding_convert("\r\n"));
			fwrite($resource, $this->encoding_convert($server."/news/".$news_info->id));
		}
		
		// 3.文章地图
		$article = DB::table('article')
				-> select('id')
				-> orderBy('id','desc')
				-> get();
		foreach( $article as $key => $article_info )
		{
			//每进来一个数组，先写入一个换行
			fwrite($resource, $this->encoding_convert("\r\n"));
			fwrite($resource, $this->encoding_convert($server."/article/".$article_info->id));
		}
		
		// 4. 图库地图
		$photos = DB::table('photos')
				-> select('id')
				-> orderBy('id','desc')
				-> get();
		foreach( $photos as $key => $photo_info )
		{
			//每进来一个数组，先写入一个换行
			fwrite($resource, $this->encoding_convert("\r\n"));
			fwrite($resource, $this->encoding_convert($server."/photoDetail/".$photo_info->id));
		}
		
		// 5. 资料库
		// 球队
		$teams = DB::table('team_info')
				-> select('team_id')
				-> orderBy('id','desc')
				-> get();
		foreach( $teams as $key => $team_info )
		{
			//每进来一个数组，先写入一个换行
			fwrite($resource, $this->encoding_convert("\r\n"));
			fwrite($resource, $this->encoding_convert($server."/teamdetail/".$team_info->team_id));
		}
		// 球员
		$players = DB::table('player_info')
				-> select('player_id')
				-> orderBy('id','desc')
				-> get();
		foreach( $players as $key => $player_info )
		{
			//每进来一个数组，先写入一个换行
			fwrite($resource, $this->encoding_convert("\r\n"));
			fwrite($resource, $this->encoding_convert($server."/playerdetail/".$player_info->player_id));
		}
		
		
		// 6.直播
		// 获取积分榜里的球队
		$scorerank = DB::table('scorerank')
					-> get();
		// 存放 所有球队的名字
		$footName = array();
		foreach( $scorerank as $key => $score )
		{
			// 球队名字
			if( !in_array($score -> name_zh,$footName) )
			{
				array_push($footName,$score -> name_zh);
			}
		}
		$live_list = DB::table('saicheng')
				-> select( 'id','c3',DB::raw(" case when league_id=2 then '英超' when league_id=3 then '西甲' when league_id=5 then '德甲' when league_id=4 then '意甲' when league_id=6 then '法甲' when league_id=128 then '中超' end as league_name "),'c4T1','c4T2',DB::raw("date_format(c2,'%m-%d') as datet") )
				-> where( 'c1','=','未开赛' )
				// 大于 此刻
				-> where( 'c2','>',date('Y-m-d H:i:s') )
				// 先按 c2
				-> orderBy('c2')
				// 先按 c3
				-> orderBy('c3','desc')
				-> get();
		$all_live = array();
		foreach( $live_list as $key=> $live_info )
		{
			if( !in_array($live_info->c4T1,$footName) || !in_array($live_info->c4T2,$footName)  )
			{
				continue;
			}else
			{
				$all_live[$key]['id']=$live_info->id;
				$all_live[$key]['c3']=$live_info->c3;
				$all_live[$key]['league_name']=$live_info->league_name;
				$all_live[$key]['c4T1']=$live_info->c4T1;
				$all_live[$key]['c4T2']=$live_info->c4T2;
				$all_live[$key]['datet']=$live_info->datet;
			}
		}
		if( count($all_live)>0 )
		{
			foreach( $all_live as $key => $live_info )
			{
				//每进来一个数组，先写入一个换行
				fwrite($resource, $this->encoding_convert("\r\n"));
				fwrite($resource, $this->encoding_convert( $server."/live_preview/".$live_info['id']) );
			}
		}
		
		
		//关闭
		fclose($resource);
		
		echo '<h1 style="color:green;" >全站地图已生成</h1><br/><a href="/showmap" style="color:red;font-size:20px;" >返回</a>';
		
	}
	
	//字符转码
	private function encoding_convert($str=''){
		//将字符串的编码从UTF-8 转到gbk
		return iconv('UTF-8','gbk',$str);
	}


}
